package cn.cdeden.job.snailjob.snailjob.domain;

import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 表示Active Directory（AD）部门的类。
 * 每个部门都有一个ID、名称、规范名称、区别名称和子部门列表。
 */
public class AdDepartment implements Serializable,Comparable<AdDepartment> {
    private String id;
    private String name;
    private String cName;
    private String distinguishedName;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getcName() {
        return cName;
    }

    public void setcName(String cName) {
        this.cName = cName;
    }

    public String getDistinguishedName() {
        return distinguishedName;
    }

    public void setDistinguishedName(String distinguishedName) {
        this.distinguishedName = distinguishedName;
    }

    public List<AdDepartment> getChildren() {
        return children;
    }

    public void setChildren(List<AdDepartment> children) {
        this.children = children;
    }

    private List<AdDepartment> children = new ArrayList<AdDepartment>();


    public AdDepartment getAdDepartmentBycName(String cName) {
        if (this.cName.equals(cName) ) {
            return this;
        }else{
            for (AdDepartment adDepartment : children) {
                AdDepartment adDepartment1 =null;
                if ((adDepartment1 = adDepartment.getAdDepartmentBycName(cName)) != null) {
                    return adDepartment1;
                }
            }
        }
        return null;
    }
    public AdDepartment getParentAdDepartmentBycName(String cName) {
        int index;
        AdDepartment adDepartment = null;
        while ((index = cName.lastIndexOf("/")) != -1) {
            cName = cName.substring(0, index);
            adDepartment = getAdDepartmentBycName(cName);
            if (adDepartment != null) {
                return adDepartment;
            }
        }
        return adDepartment;
    }

    public int compareTo(AdDepartment o) {
        return cName.length() - o.getcName().length();
    }

    public void addChildren(AdDepartment adDepartment) {
        this.children.add(adDepartment);
    }

    @Override
    public String toString() {
        return "AdDepartment{" +
            "id='" + id + '\'' +
            ", name='" + name + '\'' +
            ", cName='" + cName + '\'' +
            ", distinguishedName='" + distinguishedName + '\'' +
            ", children=" + children +
            '}';
    }
}
